
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





如何（以及为什么)使用虚假LLM[#](#how-and-why-to-use-the-fake-llm "Permalink to this headline")
==================================================================================

我们提供了一个用于测试的虚假LLM类。这使您可以模拟对LLM的调用，并模拟LLM以特定方式响应时会发生什么。

在本教程中，我们将介绍如何使用它。

我们从在代理中使用FakeLLM开始。

```python
from langchain.llms.fake import FakeListLLM

```

```python
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType

```

```python
tools = load_tools(["python_repl"])

```

```python
responses=[
    "Action: Python REPL\nAction Input: print(2 + 2)",
    "Final Answer: 4"
]
llm = FakeListLLM(responses=responses)

```

```python
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

```

```python
agent.run("whats 2 + 2")

```

```python
> Entering new AgentExecutor chain...
Action: Python REPL
Action Input: print(2 + 2)
Observation: 4

Thought:Final Answer: 4

> Finished chain.

```

```python
'4'

```

